package com.mao.action.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.struts2.json.annotations.JSON;

import com.mao.action.BaseAction;
import com.mao.action.init.InitAction;
import com.mao.action.jsonvo.product.jsonVoProductClass;
import com.mao.model.product.ProductClass;
import com.mao.service.product.IProductClassService;
import com.mao.util.ClassUtil;
import com.mao.util.SessionHelper;

public class ProductClassManageAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private IProductClassService<ProductClass> productClassService;

	private List<jsonVoProductClass> result;

	private ProductClass pc;

	private jsonVoProductClass voPc;

	Logger log = Logger.getLogger(ProductClassManageAction.class);
	
	@Override
	public String execute() {
		return SUCCESS;
	}

	// 修改分类
	public String jsonUpdateProductClasss() {
		ProductClass updatePc = productClassService.getProductClassById(pc.getId());
		updatePc.setName(pc.getName());
		updatePc.setDescription(pc.getDescription());
		if(pc.getParent().getId() != null){
			ProductClass parent = productClassService.getProductClassById(pc.getParent().getId());
			if(pc.getParent().getId() == 0){
				updatePc.setParent(null);
			}else{
				updatePc.setParent(parent);
			}
			
		}else{
			updatePc.setParent(null);
		}
		productClassService.updateProductClass(updatePc);
		updatePc.setTreePath(ClassUtil.getTreePath(updatePc));
		updatePc.setLevel(ClassUtil.getLevel(updatePc.getTreePath()));
		productClassService.updateProductClass(updatePc);
		// 更新所有下级分类的TreePath
		ClassUtil.updateTreePathAndLevel(updatePc, productClassService);
		voPc = new jsonVoProductClass();
		voPc.setId(pc.getId());
		voPc.setText(pc.getName());
		return SUCCESS;
	}

	// 新增一级分类
	public String jsonSaveProductClasss() {
		pc.setStore(SessionHelper.getSessionUser().getStore());
		pc.setParent(null);
		pc = productClassService.saveProductClass(pc);
		pc.setTreePath(ClassUtil.getTreePath(pc));
		pc.setLevel(ClassUtil.getLevel(pc.getTreePath()));
		productClassService.updateProductClass(pc);
		voPc = new jsonVoProductClass();
		voPc.setId(pc.getId());
		voPc.setText(pc.getName());
		return SUCCESS;
	}

	// 新增下级分类
	public String jsonSaveLowProductClasss() {
		long parentId = pc.getParent().getId();
		if (parentId != 0) {
			ProductClass parent = productClassService.getProductClassById(parentId);
			pc.setParent(parent);
		}
		pc.setStore(SessionHelper.getSessionUser().getStore());
		productClassService.saveProductClass(pc);
		pc.setTreePath(ClassUtil.getTreePath(pc));
		pc.setLevel(ClassUtil.getLevel(pc.getTreePath()));
		productClassService.updateProductClass(pc);
		voPc = new jsonVoProductClass();
		voPc.setId(pc.getId());
		voPc.setText(pc.getName());
		voPc.setParentId(pc.getParent().getId());
		return SUCCESS;
	}

	// 读取商品分类
	public String jsonGetAllProductClass() {
		result = new ArrayList<jsonVoProductClass>();
		List<ProductClass> pcs = null;
		Map<String,Object> attsMap = null;
		jsonVoProductClass jpc = null;
		// 初始页面
		if (pc == null) {
			pcs = productClassService.getAllProductClass(SessionHelper.getSessionUser().getStore().getId());
		} else {
			// 读取下级
			pc = productClassService.getProductClassById(pc.getId());
			Set<ProductClass> pcsets = pc.getLowClass();
			pcs = new ArrayList<ProductClass>();
			Iterator<ProductClass> it = pcsets.iterator();
			while (it.hasNext()) {
				pcs.add(it.next());
			}
		}
		for (int i = 0; i < pcs.size(); i++) {
			attsMap = new HashMap<String,Object>();
			jpc = new jsonVoProductClass();
			pc = pcs.get(i);
			jpc.setId(pc.getId());
			jpc.setText(pc.getName());
			jpc.setState("closed");
			if(pc.getLevel() != 1){
				attsMap.put("parentId", pc.getParent().getId());
				attsMap.put("parentName", pc.getParent().getName());
			}
			attsMap.put("description", pc.getDescription());
			attsMap.put("treepath", pc.getTreePath());
			jpc.setAttributes(attsMap);
			result.add(jpc);
		}
		return SUCCESS;
	}

	// 删除分类
	public String jsonDeleteProductClassById() {
		ProductClass proClass = productClassService.getProductClassById(pc.getId());
		proClass.setStatus(ProductClass.STATUS_DELETED);
		pc = productClassService.updateProductClass(proClass);
		voPc = new jsonVoProductClass();
		voPc.setId(pc.getId());
		voPc.setText(pc.getName());
		return SUCCESS;
	}

	
	
	public String jsonGetProductClassById() {
		pc = productClassService.getProductClassById(pc.getId());
		return SUCCESS;
	}

	@JSON(serialize = false)
	public IProductClassService<ProductClass> getProductClassService() {
		return productClassService;
	}

	public void setProductClassService(IProductClassService<ProductClass> productClassService) {
		this.productClassService = productClassService;
	}

	public ProductClass getPc() {
		return pc;
	}

	public void setPc(ProductClass pc) {
		this.pc = pc;
	}

	public List<jsonVoProductClass> getResult() {
		return result;
	}

	public void setResult(List<jsonVoProductClass> result) {
		this.result = result;
	}

	public jsonVoProductClass getVoPc() {
		return voPc;
	}

	public void setVoPc(jsonVoProductClass voPc) {
		this.voPc = voPc;
	}
	

}
